package com.imc.platform.base.appointment.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.appointment.pojo.Appointment;
import com.imc.platform.base.appointment.service.IAppointmentService;
import com.imc.platform.base.org.service.IOrgService;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*
* 类描述：预约控制类
* 开发者：YT
* 创建日期：2018/10/27 21:56
*/
@Slf4j
@Controller
@RequestMapping("base/appointment")
public class AppointmentController {

    @Autowired
    IAppointmentService appointmentService;

    @Autowired
    MysqlUtil mysqlUtil;

    @Autowired
    private IUserMapper userMapper;

    @Autowired
    IOrgService iOrgService;

    /**
     *QY 2018/9/3 8:01
     *方法描述：传参对象
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple{
        Map<String,String> parmsMap;
    }

    /*
    * YT  2018/10/27 21:57
    * 方法描述：查询预约列表
    */
    @RequestMapping(value = "/queryAppointmentList", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryAppointmentList(Principal principal,Pageable pageable,@RequestParam String approverId,@RequestParam String orgId,@RequestParam String lessonId,@RequestParam String userId,@RequestParam String lessonScheduleId,@RequestParam String startTime,@RequestParam String endTime){
        String uname = principal.getName();
        User user = userMapper.queryRoleUserByLoginName(uname);
        String currentorgId = iOrgService.queryOrgIdByUserId(String.valueOf(user.getId()));
        Map<String,String> mapP = new HashMap<>();
        mapP.put("orgId", orgId);
        mapP.put("lessonId",lessonId);
        mapP.put("lessonScheduleId",lessonScheduleId);
        mapP.put("userId",userId);
        mapP.put("approverId",approverId);
        mapP.put("startTime",startTime);
        mapP.put("endTime",endTime);
        mapP.put("currentorgId", currentorgId);
        //4是老师
        if("master".equals(user.getDuty())){
            mapP.put("dutyFlag", "1");
        }
        if("branch".equals(user.getDuty())){
            mapP.put("dutyFlag", "2");
        }
        if("teacher".equals(user.getDuty())){
            mapP.put("dutyFlag", "3");
        }
        if("admin".equals(user.getDuty())){
            mapP.put("dutyFlag", "0");
        }
        //4是老师
        mapP.put("roleId",user.getRoleId());
        mapP.put("currentUserId",String.valueOf(user.getId()));
        PageInfo<Appointment> queryList=appointmentService.queryAppointmentByMap(pageable.getPageNumber(),pageable.getPageSize(),mapP);
        for(Appointment c : queryList.getList()){
            c.setPageNumber(pageable.getPageNumber());
            c.setPageSize(pageable.getPageSize());
        }
        JSONObject map = new JSONObject();
        map.put("total",queryList.getTotal());
        map.put("rows",queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /*
    * YT  2018/10/27 21:57
    * 方法描述：预约菜单首页跳转
    */
    @GetMapping("/appointmentQuery")
    public String appointmentQuery(){
        return "base/appointment/appointment";
    }

    /*
    * YT  2018/10/17 23:34
    * 方法描述：查询所有的预约信息
    */
    @RequestMapping(value = "/queryAppointmentByMap", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryAppointmentByMap(Principal principal,@RequestBody ReqTuple reqTuple) {
        String uname = principal.getName();
        User user = mysqlUtil.getUserByLoginNameCommon(uname);
        Map<String,String> parmsM = reqTuple.getParmsMap();
        PageInfo <Appointment> queryList = appointmentService.queryAppointmentByMap(Integer.parseInt(reqTuple.getParmsMap().get("page")), Integer.parseInt(reqTuple.getParmsMap().get("size")),parmsM);
        for(Appointment c : queryList.getList()){
            c.setPageNumber(Integer.parseInt(reqTuple.getParmsMap().get("page")));
            c.setPageSize(Integer.parseInt(reqTuple.getParmsMap().get("size")));
        }
        JSONObject map = new JSONObject();
        map.put("total", queryList.getTotal());
        map.put("rows", queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /*
    * YT  2018/10/17 23:36
    * 方法描述：查看预约信息页面
    */
    @RequestMapping(value = "/showSAppointmentPage", method = RequestMethod.GET)
    public String showSAppointmentPage(@RequestParam String id, Model model) {
        model.addAttribute("id", id);
        return "base/appointment/showAppointment";
    }

    /*
    * YT  2018/10/17 23:38
    * 方法描述：根据ID查询预约信息
    */
    @RequestMapping(value = "/queryAppointmentInfoById", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryAppointmentInfoById(@RequestParam String id) {
        return ResultDTO.builder()
                .data(appointmentService.queryAppointmentInfoById(id))
                .build();
    }

    /*
    * YT  2018/10/27 21:57
    * 方法描述：添加预约信息
    */
    @RequestMapping(value = "/addAppointment",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addAppointment(@RequestBody ReqTuple reqTuple) throws Exception{
        Map<String,String> paramMap = reqTuple.getParmsMap();
        Appointment bn = new Appointment();
        bn.setOrgId(paramMap.get("orgId"));
        bn.setLessonId(paramMap.get("lessonId"));
        bn.setApproverId(paramMap.get("approverId"));
        bn.setLessonScheduleId(paramMap.get("lessonScheduleId"));
        bn.setUserId(paramMap.get("userId"));
        bn.setDealTime(paramMap.get("dealTime"));
        bn.setState("0");
        bn.setRemark(paramMap.get("remark"));
        bn.setSubscriber(paramMap.get("subscriber"));
        String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        bn.setReservationDate(dateStr);
        int count = appointmentService.addAppointment(bn);
        int bnId = bn.getId();
        JSONObject map = new JSONObject();
        map.put("bnId",bnId);
        ResultDTO obj = ResultDTO.builder().data(map).build();
        return obj;
    }

    /**
     * QY 2018/9/3 10:16
     * 方法描述：更改状态
     **/
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO updateState(@RequestParam String state, @RequestParam String id) {
        return ResultDTO.builder()
                .data(appointmentService.updateState(state, id))
                .build();
    }

}